package com.scau402.common.utils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau402.common.constant.HttpStatus;
import com.scau402.common.core.page.PageDomain;
import com.scau402.common.core.page.TableDataInfo;
import com.scau402.common.core.page.TableSupport;
import com.scau402.common.utils.sql.SqlUtil;
import org.slf4j.MDC;

import java.util.List;
import java.util.Optional;

/**
 * 分页工具类
 * 
 * @author ruoyi
 */
public class PageUtils extends PageHelper
{
    /**
     * 设置请求分页数据
     */
    public static void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }

    /**
     * 响应请求分页数据
     * list为封装后的ArrayList列表对象，list2为startPage()后得到的Page列表对象，两者数据条数需保持一致
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static TableDataInfo getDataTableFrom2Lists(List<?> list, List<?> list2) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        PageInfo pageInfo = new PageInfo(list2);
        PageUtils.changePageInfo(pageInfo);
        rspData.setTotal(pageInfo.getTotal());
        rspData.setPageSize(pageInfo.getPageSize());
        rspData.setCurrentPage(pageInfo.getPageNum());
        return rspData;
    }

    /**
     * 响应请求分页数据
     * @param list 数据列表
     */
    public static TableDataInfo getDataTableFromList(List<?> list){
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(list.size());
        return rspData;
    }

    /**
     * 将分页信息放入MDC
     * @param page
     * @param <T>
     */
    public static <T extends Page> void fetchSessionPageInfo(T page) {
        MDC.put("total", String.valueOf(page.getTotal()));
        MDC.put("pageNum", String.valueOf(page.getPageNum()));
        MDC.put("pageSize", String.valueOf(page.getPageSize()));
    }

    /**
     * 将分页信息放入MDC
     * @param pageInfo
     */
    public static void fetchSessionPageInfo(PageInfo<?> pageInfo) {
        MDC.put("total", String.valueOf(pageInfo.getTotal()));
        MDC.put("pageNum", String.valueOf(pageInfo.getPageNum()));
        MDC.put("pageSize", String.valueOf(pageInfo.getPageSize()));
    }

    public static PageInfo<?> changePageInfo(PageInfo<?> pageInfo) {
        // 如果有设置分页信息在MDC，则覆盖原来的分页信息（既然设置在MDC，那么就是要覆盖的意思）
        Optional.ofNullable(MDC.get("pageNum")).ifPresent(pageNum -> pageInfo.setPageNum(Integer.parseInt(pageNum)));
        Optional.ofNullable(MDC.get("pageSize")).ifPresent(pageSize -> pageInfo.setPageSize(Integer.parseInt(pageSize)));
        Optional.ofNullable(MDC.get("total")).ifPresent(total -> pageInfo.setTotal(Long.parseLong(total)));
        return pageInfo;
    }
}
